package com.sz.jinzheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sz.jinzheng.mapper.StaffExminationMapper;
import com.sz.jinzheng.mapper.TaskMapper;
import com.sz.jinzheng.model.PageBean;
import com.sz.jinzheng.model.StaffExmination;
import com.sz.jinzheng.model.Task;
import com.sz.jinzheng.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private StaffExminationMapper staffExminationMapper;

    @Override
    public PageBean<Task> getTaskByExam(Task task, int index, int size) {
        //利用mybatis分页插件来进行分页数据库
        PageHelper.startPage(index, size);
        List<Task> tasks = taskMapper.getTaskByExam(task);
        Page<Task> page = (Page<Task>) tasks;
        PageBean<Task> taskPageBean = new PageBean<>(page.getTotal(),
                page.getPageSize(), page.getPageNum(), page.getPages(), page.getResult());
        return taskPageBean;
    }

    @Transactional
    @Override
    public int updateTaskStatus(Task task) {
        int i = taskMapper.updateTaskStatus(task);
        int i2 = 0;
        log.info("updateTaskStatus==============task的值为："+task);
        if (i>0){
           i2= staffExminationMapper.addStaffExmination(new StaffExmination(task.getUserId(),task.getId(),task.getStatus()));
        }
        return i2;
    }

    @Override
    public PageBean<Task> getMyCompletingTask(Task task, int index, int size) {
        //利用mybatis分页插件来进行分页数据库
        PageHelper.startPage(index, size);
        List<Task> tasks = taskMapper.getMyCompletingTask(task);
        Page<Task> page = (Page<Task>) tasks;
        PageBean<Task> taskPageBean = new PageBean<>(page.getTotal(),
                page.getPageSize(), page.getPageNum(), page.getPages(), page.getResult());
        return taskPageBean;
    }
}
